// 存储键名常量
const STORAGE_KEY = 'word_groups_progress';

/**
 * 分组学习进度的数据结构
 * @typedef {Object} GroupProgress
 * @property {string} groupId - 分组ID
 * @property {string} groupName - 分组中文名称
 * @property {boolean} isCompleted - 分组是否学习完成
 * @property {string} lastWordId - 当前学习到的最后一个单词ID
 * @property {boolean} hasWordTest - 是否进行过识单词测试
 * @property {Object.<string, boolean>} wordTestResults - 识单词测试结果，key为单词ID，value为是否正确
 * @property {boolean} hasListeningTest - 是否进行过听单词测试
 * @property {Object.<string, boolean>} listeningTestResults - 听单词测试结果，key为单词ID，value为是否正确
 * @property {number} lastStudyTime - 最近一次学习时间戳
 */

/**
 * 获取存储的所有分组进度
 * @returns {Object.<string, GroupProgress>} 所有分组的进度信息
 */
const getAllGroupsProgress = () => {
  try {
    const data = wx.getStorageSync(STORAGE_KEY);
    return data || {};
  } catch (e) {
    console.error('获取分组进度失败:', e);
    return {};
  }
};

/**
 * 获取单个分组的进度
 * @param {string} groupId - 分组ID
 * @returns {GroupProgress|null} 分组进度信息
 */
const getGroupProgress = (groupId) => {
  const allProgress = getAllGroupsProgress();
  return allProgress[groupId] || null;
};

/**
 * 更新分组进度
 * @param {string} groupId - 分组ID
 * @param {Partial<GroupProgress>} progressData - 需要更新的进度数据
 * @returns {boolean} 更新是否成功
 */
const updateGroupProgress = (groupId, progressData) => {
  try {
    const allProgress = getAllGroupsProgress();
    const currentProgress = allProgress[groupId] || {
      groupId,
      groupName: '',
      isCompleted: false,
      lastWordId: '',
      hasWordTest: false,
      wordTestResults: {},
      hasListeningTest: false,
      listeningTestResults: {},
      lastStudyTime: 0
    };

    allProgress[groupId] = {
      ...currentProgress,
      ...progressData,
      lastStudyTime: new Date().getTime()
    };

    wx.setStorageSync(STORAGE_KEY, allProgress);
    return true;
  } catch (e) {
    console.error('更新分组进度失败:', e);
    return false;
  }
};

/**
 * 更新分组的单词测试结果
 * @param {string} groupId - 分组ID
 * @param {string} wordId - 单词ID
 * @param {'word'|'listening'} testType - 测试类型
 * @param {boolean} isCorrect - 是否正确
 * @returns {boolean} 更新是否成功
 */
const updateWordTestResult = (groupId, wordId, testType, isCorrect) => {
  const groupProgress = getGroupProgress(groupId) || {
    wordTestResults: {},
    listeningTestResults: {},
    hasWordTest: false,
    hasListeningTest: false
  };

  if (testType === 'word') {
    groupProgress.wordTestResults = {
      ...groupProgress.wordTestResults,
      [wordId]: isCorrect
    };
    groupProgress.hasWordTest = true;
  } else if (testType === 'listening') {
    groupProgress.listeningTestResults = {
      ...groupProgress.listeningTestResults,
      [wordId]: isCorrect
    };
    groupProgress.hasListeningTest = true;
  }

  return updateGroupProgress(groupId, groupProgress);
};

/**
 * 设置分组学习完成状态
 * @param {string} groupId - 分组ID
 * @param {string} groupName - 分组名称
 * @returns {boolean} 更新是否成功
 */
const setGroupCompleted = (groupId, groupName) => {
  return updateGroupProgress(groupId, {
    groupId,
    groupName,
    isCompleted: true
  });
};

export default {
  getAllGroupsProgress,
  getGroupProgress,
  updateGroupProgress,
  updateWordTestResult,
  setGroupCompleted
};
